package com.mycompany.ui.panel;

import org.springframework.beans.factory.annotation.Autowired;

import com.mycompany.config.Injector;
import com.mycompany.model.Categoria;
import com.mycompany.model.Prodotto;
import com.mycompany.service.CategoriaService;
import com.mycompany.service.ProdottoService;
import com.vaadin.data.fieldgroup.BeanFieldGroup;
import com.vaadin.data.fieldgroup.FieldGroup;
import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
import com.vaadin.data.fieldgroup.PropertyId;
import com.vaadin.data.util.BeanItem;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.event.Action;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.Action.Handler;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.ui.Button;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Notification.Type;

public class ProdottoPanel extends HorizontalSplitPanel{
	
	@PropertyId("codice")
	private TextField codiceField = new TextField("Codice");
	@PropertyId("descrizione")
	private TextField descrzioneField = new TextField("Descrizione");
	@PropertyId("categoria")
	private ComboBox comboBoxCategorie = new ComboBox("Categoria");
	
	private BeanItemContainer<Categoria> containerComboBoxCategorie = new BeanItemContainer<Categoria>(Categoria.class);;
	
	private Table tabellaProdotti;
	private BeanItemContainer<Prodotto> containerTabellaProdotti = new BeanItemContainer<Prodotto>(Prodotto.class);
	private FieldGroup fieldGroup;
	
	@Autowired
	ProdottoService prodottoService;
	
	@Autowired
	CategoriaService categoriaService;
	
	private Action actionDelete = new Action("Delete");

	
	public ProdottoPanel() {
		// TODO Auto-generated constructor stub
		Injector.inject(this);
		setFirstComponent(createTabellaProdotti());
		setSecondComponent(createFromProdotto());
		setSplitPosition(70, Unit.PERCENTAGE);
	}


	private Component createTabellaProdotti() {
		// TODO Auto-generated method stub
		tabellaProdotti = new Table();
		tabellaProdotti.setSizeFull();
		tabellaProdotti.setSelectable(true);
		fillContainerTabella();
		tabellaProdotti.setContainerDataSource(containerTabellaProdotti);
		tabellaProdotti.setVisibleColumns(new Object [] {"codice","descrizione","categoria"});
		
		tabellaProdotti.addItemClickListener(new ItemClickListener() {
			
			@Override
			public void itemClick(ItemClickEvent event) {
				// TODO Auto-generated method stub
				if(event.isDoubleClick())
					fieldGroup.setItemDataSource(event.getItem());
				
			}
		});
		
		tabellaProdotti.addActionHandler(new Handler() {

			@Override
			public void handleAction(Action action, Object sender, Object target) {
				// TODO Auto-generated method stub
				if(actionDelete == action){
					final Prodotto prodottoToRemove = (Prodotto) target;
					containerTabellaProdotti.removeItem(prodottoToRemove);
					prodottoService.deleteProdotto(prodottoToRemove);
					Notification.show("Prodotto Eliminata");
				}
			}

			@Override
			public Action[] getActions(Object target, Object sender) {
				// TODO Auto-generated method stub
				return new Action [] {actionDelete};
			}
		});
		
		return tabellaProdotti;
	}

	public void fillContainerTabella() {
		
		containerTabellaProdotti.removeAllItems();
		
		// TODO Auto-generated method stub
		for(Prodotto prodotto : prodottoService.findAll())
			containerTabellaProdotti.addBean(prodotto);
		
		
	}
	
	private Component createFromProdotto() {
		// TODO Auto-generated method stub
		
		fieldGroup = new BeanFieldGroup<Prodotto>(Prodotto.class);
		fieldGroup.setItemDataSource(new BeanItem<Prodotto>(new Prodotto(new Long(0),"")));
		fieldGroup.bindMemberFields(this);
		
		FormLayout formLayout = new FormLayout();
		formLayout.addComponent(codiceField);
		formLayout.addComponent(descrzioneField);
		comboBoxCategorie.setContainerDataSource(containerComboBoxCategorie);
		fillContainerComboBox();
		formLayout.addComponent(comboBoxCategorie);
		formLayout.addComponent(createButtonSave());
		
		return formLayout;
	}


	private Component createButtonSave() {
		
		Button button = new Button("Salva");
		button.addClickListener(new Button.ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				// TODO Auto-generated method stub
				try {
					
					fieldGroup.commit();
					fieldGroup.setBuffered(false);
					BeanItem<Prodotto> prodottoToSave = (BeanItem<Prodotto>) fieldGroup.getItemDataSource();
					prodottoService.save(prodottoToSave.getBean());
					containerTabellaProdotti.addBean(prodottoToSave.getBean());
					Notification.show("Prodotto Salvato");
					
					fieldGroup.setItemDataSource(new BeanItem<Prodotto>(new Prodotto(new Long(0),"")));
				
				} catch (CommitException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					Notification.show(e.getMessage(), Type.ERROR_MESSAGE);

				}
			}
		});
		
		return button;
	}


	public void fillContainerComboBox() {
		// TODO Auto-generated method stub
		containerComboBoxCategorie.removeAllItems();
		
			for(Categoria categoria : categoriaService.findAll())
				containerComboBoxCategorie.addBean(categoria);
	}

}
